package com.lcx.config;




import java.io.Serializable;


public class R<T> implements Serializable {
    private String host;
    private Integer code;
    private String errorMessage;
    private T data;

    public R () {
        this.code = 200;
    }

    public R (Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public R (Integer code, String msg, T data) {
        this.code = code;
        this.errorMessage = msg;
        this.data = data;
    }

    public R (Integer code, String msg) {
        this.code = code;
        this.errorMessage = msg;
    }

    public static R errorResult(int code, String msg) {
        R result = new R();
        return result.error(code, msg);
    }

    public static R okResult(int code, String msg) {
        R result = new R();
        return result.ok(code, null, msg);
    }

    public static R okResult(Object data) {
        R result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static R errorResult(AppHttpCodeEnum enums) {
        return setAppHttpCodeEnum(enums, enums.getErrorMessage());
    }

    public static R errorResult(AppHttpCodeEnum enums, String errorMessage) {
        return setAppHttpCodeEnum(enums, errorMessage);
    }

    public static R setAppHttpCodeEnum(AppHttpCodeEnum enums) {
        return okResult(enums.getCode(), enums.getErrorMessage());
    }

    private static R setAppHttpCodeEnum(AppHttpCodeEnum enums, String errorMessage) {
        return okResult(enums.getCode(), errorMessage);
    }

    public R<?> error(Integer code, String msg) {
        this.code = code;
        this.errorMessage = msg;
        return this;
    }

    public R<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public R<?> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.errorMessage = msg;
        return this;
    }

    public R<?> ok(T data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }


    public static void main(String[] args) {
        //前置
//        AppHttpCodeEnum success = AppHttpCodeEnum.SUCCESS;
//        System.out.println(success.getCode());
//        System.out.println(success.getErrorMessage());

        //查询一个对象
//        Map map = new HashMap();
//        map.put("name","zhangsan");
//        map.put("age",18);
//        ResponseResult result = ResponseResult.okResult(map);
//        System.out.println(JSON.toJSONString(result));


        //新增，修改，删除  在项目中统一返回成功即可
//        ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
//        System.out.println(JSON.toJSONString(result));


        //根据不用的业务返回不同的提示信息  比如：当前操作需要登录、参数错误
//        ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
//        System.out.println(JSON.toJSONString(result));

        //查询分页信息
//        PageResponseResult responseResult = new PageResponseResult(1,5,50);
//        List list = new ArrayList();
//        list.add("itcast");
//        list.add("itheima");
//        responseResult.setData(list);
//        System.out.println(JSON.toJSONString(responseResult));

    }
}
